package xupt.se.ttms.view.seat;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import xupt.se.ttms.model.Movie;
import xupt.se.ttms.model.Studio;
import xupt.se.ttms.service.StudioSrv;
import xupt.se.ttms.view.customer.Customer_InformationUI;
import xupt.se.ttms.view.sellticket.TicketOkUI;





public class Seat_Choose {
	public static int flag = 0;
	public static JFrame frame;
	public static int m,n,k,i,j;
	public static int row;
	public static int col;
	public static int s=m*n;
	public static String texts;
    public static String[] seat ;
    public static JButton[][] buttons;
    public static int[][] st;
    public static String studio_name;
    public static String studio_intro;
    public static int[][] str;
    public static Movie movies;
    public static String time;
    public static int tnp = 0;
    public Seat_Choose(String name, int q[][],Movie movie,String times) throws Exception
    {
    	movies = movie;
    	time = times;
    	frame = new JFrame();
    	StudioSrv studioSrv=new StudioSrv();
    	List<Studio> studios=new ArrayList<>();
    	studios=studioSrv.Fetch(name);
    	if(studios.size()<1)
    		return;
    	m=studios.get(0).getRowCount();
    	n=studios.get(0).getColCount();
        s=m*n;
        studio_name=name;
        studio_intro=m+"X"+n;
        // 设置窗口标题

    	frame.setTitle("选座界面");
        init(q);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0, 0, 850, 500);
        frame.getContentPane().setBackground(Color.decode("#FFe4c4"));
        // 窗体大小不能改变
        frame.setResizable(false);
        // 居中显示
        frame.setLocationRelativeTo(null);
        // 窗体可见
        frame.setVisible(true);
       
    }
    public void init(int q[][])
    {
    	st = new int[m][n];
    	buttons = new JButton[m][n];
    	seat=new String[s];
    	 JPanel jpanel =new JPanel();
    	 jpanel.setBackground(Color.magenta);
    	 jpanel.setOpaque(false);
    	 JLabel aa = new JLabel();
         aa.setBackground(Color.white);
         aa.setOpaque(true);
         aa.setPreferredSize(new Dimension(30,30));
         JLabel bb = new JLabel("可选座位");
         bb.setBounds(385,60,75,30);
         aa.setBounds(350,60,30,30);
         JLabel cc = new JLabel();
         cc.setOpaque(true);
         cc.setBackground(Color.gray);
         cc.setPreferredSize(new Dimension(30,30));
         cc.setBounds(450,60,30,30);
         JLabel dd = new JLabel("以选座位");
         dd.setBounds(485,60,75,30);
         JLabel ff=new JLabel(studio_name);
         ff.setBounds(350,10,100,20);
         JLabel ff1=new JLabel();
         ff1.setBounds(450,10,100,20);
         JLabel gg =new JLabel(studio_intro);
         gg.setBounds(350,30,100,20);
         JLabel gg1=new JLabel();
         gg1.setBounds(450,30,100,20);
         jpanel.add(gg1);
         jpanel.add(gg);
         jpanel.add(ff1);
         jpanel.add(ff);
         jpanel.add(aa);
         jpanel.add(bb);
         jpanel.add(cc);
         jpanel.add(dd);
         JLabel b = new JLabel("小霸王影院");
         b.setBounds(145, 100, 560, 30);
         b.setHorizontalAlignment(SwingConstants.CENTER);
         b.setPreferredSize(new Dimension(560,30));
         b.setBorder(BorderFactory.createLineBorder(Color.red));
 
         jpanel.add(b);
         int z=135-(12+(350-20*n)/(n-1));
         int z1=12+(350-20*n)/(n-1);
         int x,x1;
         k=0;
         for(i=0;i<n;i++)
         {
        	  z=z+z1;
        	  x=145-(30+(560-30*m)/(m-1));
        	  x1=30+(560-30*m)/(m-1);
        	 for(j=0;j<m;j++)
        	 { 
        		 st[i][j] = 0;
        		 x=x1+x;
        		 row = i;
        		 col = j;
        		 if(q[i][j]==0)
        		 {
        			 System.out.println(1);
        			 JButton a = new JButton();
            		 a.setBackground(Color.white);
            		 a.setPreferredSize(new Dimension(30,20));
            		 a.setBounds(x,z,30,20);
            		 buttons[i][j] = a; 
            		 a.addActionListener(new ActionListener() {
            	  			public void actionPerformed(ActionEvent Event) {
            	  				if(a.getBackground().equals(Color.white))
            	  				{
            	  					st[row][col]=1;
            	  					k++;
            	  					a.setBackground(Color.gray);
            	  				}
            	  				else if(a.getBackground().equals(Color.gray))
            	  				{
            	  					a.setBackground(Color.white);
            	  					k--;
            	  					st[row][col] = 0;
            	  				}
            	  				else{
            	  					return;
            	  				}
            	  			}
            	  	});
            		 jpanel.add(a);
        		 }
        		 if(q[i][j]==1)
        		 {
        			 System.out.println(1);
        			 JButton a = new JButton();
            		 a.setBackground(Color.green);
            		 a.setPreferredSize(new Dimension(30,20));
            		 a.setBounds(x,z,30,20);
            		 buttons[i][j] = a; 
            		 a.addActionListener(new ActionListener() {
         	  			public void actionPerformed(ActionEvent Event) {
         	  				if(a.getBackground().equals(Color.white))
         	  				{
         	  					st[i][j]=1;
         	  					k++;
         	  					a.setBackground(Color.gray);
         	  				}
         	  				else if(a.getBackground().equals(Color.gray))
         	  				{
         	  					k--;
         	  					st[i][j] = 0;
         	  					a.setBackground(Color.white);
         	  				}
         	  				else{
         	  					return;
         	  				}
         	  			}
         	  	});
            		 jpanel.add(a);
        		 }
        		 if(q[i][j]==2)
        		 {
        			 System.out.println(1);
        			 JButton a = new JButton();
            		 a.setBackground(Color.red);
            		 a.setPreferredSize(new Dimension(30,20));
            		 a.setBounds(x,z,30,20);
            		 buttons[i][j] = a; 
            		 a.addActionListener(new ActionListener() {
         	  			public void actionPerformed(ActionEvent Event) {
         	  				if(a.getBackground().equals(Color.white))
         	  				{
         	  					k++;
         	  					st[i][j]=1;
         	  					a.setBackground(Color.gray);
         	  				}
         	  				else if(a.getBackground().equals(Color.gray))
         	  				{
         	  					k--;
         	  					st[row][col] = 0;
         	  					a.setBackground(Color.white);
         	  				}
         	  				else{
         	  					return;
         	  				}
         	  			}
         	  	});
            		 jpanel.add(a);
        		 }
        	 }
         }
         JButton q1 = new JButton("确认选票");
         q1.setPreferredSize(new Dimension(200,30));
         q1.setBackground(Color.magenta);
         q1.setOpaque(false);
         q1.setFocusPainted(false) ;
         q1.addActionListener(new ActionListener() {
  			public void actionPerformed(ActionEvent Event) {
  				str = new int[k][2];
  				for(int i=0;i<n;i++){
  					for(int j=0;j<m;j++)
  					{
  						if(buttons[i][j].getBackground().equals(Color.gray)){
  							str[tnp][0] = i+1;
  							str[tnp][1] = j+1;
  							tnp++;
  						}
  					}
  					
  				}
  				tnp=0;
  				try {
  					new TicketOkUI(movies, str, studio_name, time, k, 2);
				
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
		
				
  			}
  		});
         q1.setBounds(225,440,200,30);
         JButton q2 = new JButton("返回");
         q2.setPreferredSize(new Dimension(200,30));
         q2.addActionListener(new ActionListener() {
  			public void actionPerformed(ActionEvent Event) {
  				frame.dispose();
  			}
  		});
         q2.setBackground(Color.magenta);
         q2.setOpaque(false);
         q2.setFocusPainted(false) ;
         q2.setBounds(425,440,200,30);
         jpanel.add(q1);
         jpanel.add(q2);
         jpanel.setLayout(null);
         frame.add(jpanel);
    }
	}